//
//  HWWelfareCenterController.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/4/2.
//  Copyright © 2022 94Wan. All rights reserved.
//

import UIKit
import JXSegmentedView
import JXPagingView
class HWWelfareCenterController: HWBaseViewController {
    var pagingView:JXPagingView!
    var segmentBgView:UIView!
    var segmentedView: JXSegmentedView = JXSegmentedView(frame: CGRect.zero)
    var segmentedDataSource: JXSegmentedTitleDataSource!
    var headView:HWWelfareCenterHeaderView!
    
    var tableView:UITableView!
    let tableViewProtocol = HWWelfareCenterProtocol()
    let httpManager = HWWelfareCenterHttpManager()
    var welfareModel = HWWelfareCenterModel()
    var welfareNewsModels:[HWWelfareCenterNewsModel] = []
    var titles:[String] = []
    
    var statusBarStyle:UIStatusBarStyle = .default
    
    override var preferredStatusBarStyle: UIStatusBarStyle {
        let _ = super.preferredStatusBarStyle
        return self.statusBarStyle
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        initUI()
        
        initFrame()
        
        loadData()
        
        addAction()
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        pagingView.mainTableView.setContentOffset(CGPoint(x: 0, y: 0), animated: true)
        self.statusBarStyle = .lightContent
        setNeedsStatusBarAppearanceUpdate()
        
        self.httpManager.getWelfareFunc { model in
            self.welfareModel = model
            self.headView.model = self.welfareModel
        } failure: { errorString in

        }
    }

}

extension HWWelfareCenterController {
    private func initUI() -> Void {
        navigationView.titleLabel.text = "福利"
        navigationView.titleLabel.textColor = .white
        navigationView.backButton.isHidden = true
        navigationView.layoutIfNeeded()
        UIColor.gradientColor(view: navigationView, colors: [UIColor.colorWithHexString(hexString: "#494A56").cgColor,UIColor.colorWithHexString(hexString: "#20202A").cgColor], start: CGPoint(x: 0, y: 0), end: CGPoint(x: 1, y: 0))
        
        UIColor.gradientColor(view: view, colors: [UIColor.colorWithHexString(hexString: "#494A56").cgColor,UIColor.colorWithHexString(hexString: "#20202A").cgColor], start: CGPoint(x: 0, y: 0), end: CGPoint(x: 1, y: 0))
        
        headView = HWWelfareCenterHeaderView()
        headView.frame = CGRect(x: 0, y: 0, width: screenWidth, height: 182*widthScale)
        
        segmentBgView = UIView()
        segmentBgView.backgroundColor = .white
        
        segmentedDataSource = JXSegmentedTitleDataSource()
        segmentedDataSource.titles = []
        segmentedDataSource.isTitleColorGradientEnabled = true
        segmentedDataSource.titleNormalColor = UIColor.colorWithHexString(hexString: "#333333")
        segmentedDataSource.titleSelectedColor = UIColor.colorWithHexString(hexString: "#333333")
        segmentedDataSource.titleNormalFont = UIFont.systemFont(ofSize: 14*widthScale)
        segmentedDataSource.titleSelectedFont = UIFont.boldSystemFont(ofSize: 18*widthScale)
        segmentedDataSource.isItemSpacingAverageEnabled = true
        
        segmentedView.dataSource = segmentedDataSource
        let indicator = JXSegmentedIndicatorLineView()
        indicator.indicatorHeight = 4*widthScale
        indicator.verticalOffset = 0
        indicator.indicatorColor = .colorWithHexString(hexString: "#FF726A")
        
        segmentedView.indicators = [indicator]
        segmentedView.frame = CGRect(x: 0, y: 0, width: screenWidth, height: 35*widthScale)
        segmentedView.backgroundColor = .colorWithHexString(hexString: "#FFFFFF")
        segmentBgView.addSubview(self.segmentedView)
        
        
        pagingView = JXPagingView(delegate: self)
        pagingView.automaticallyDisplayListVerticalScrollIndicator = false
        pagingView.mainTableView.backgroundColor = .white
        pagingView.listContainerView.isCategoryNestPagingEnabled = true
        view.addSubview(pagingView)
        segmentedView.listContainer = pagingView.listContainerView
    }
    
    private func initFrame() -> Void {

        pagingView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(navigationView.snp.bottom)
        }
        
        segmentedView.snp.makeConstraints { make in
            make.left.right.top.equalToSuperview()
            make.height.equalTo(35*widthScale)
        }
    }
}

extension HWWelfareCenterController {
    func loadData() -> Void {
        
        let netGroup = DispatchGroup()
        let workingQueue = DispatchQueue(label: "welfareCenter")
        
        netGroup.enter()
        workingQueue.async {
            self.httpManager.getWelfareFunc { model in
                self.welfareModel = model
                netGroup.leave()
            } failure: { errorString in
                netGroup.leave()
            }
        }
        
        netGroup.notify(queue: workingQueue) {
            DispatchQueue.main.asyncAfter(deadline: .now()) {
                self.headView.model = self.welfareModel
                self.titles.removeAll()
                for mod in self.welfareModel.renwu {
                    self.titles.append(mod.title)
                }
                self.segmentedDataSource.titles = self.titles
                self.segmentedView.reloadData()
                self.pagingView.reloadData()
                self.pagingView.mainTableView.backgroundColor = .clear
            }
            
        }

    }
}

extension HWWelfareCenterController {
    func addAction() -> Void {
        self.headView.itemClick = {
            mod in
            if mod.login == "1" {
                if HWLocallyStoredData.getLoginUserID().count>0{
                    HWUniversal.pushController(type: mod.type, urlString: mod.url)
                }else{
                    let loginController = HWLoginController()
                    self.navigationController?.pushViewController(loginController, animated: true)
                }
            }else {
                HWUniversal.pushController(type: mod.type, urlString: mod.url)
            }
            
        }
        
        self.tableViewProtocol.jumpToNews = {
            mod in
            let webView = HWWebViewGameViewController()
            webView.urlString = HWGlobalConst.shared.baseUrl + "?ct=aznews&ac=info&id=" + mod.id + "&language=" + HWjianzhiduiObject.returnLanguageAcquisition()
            webView.gname = mod.title
            self.navigationController?.pushViewController(webView, animated: true)
        }
    }
}


//extension JXPagingListContainerView: JXSegmentedViewListContainer {}
extension HWWelfareCenterController:JXPagingViewDelegate {
    func pagingView(_ pagingView: JXPagingView, initListAtIndex index: Int) -> JXPagingViewListViewDelegate {
        if self.welfareModel.renwu[index].type == "0" {
            let everyDay = HWEveryDayTaskController()
            return everyDay
        } else if self.welfareModel.renwu[index].type == "1" {
            let growthPath = HWGrowthPathController()
            return growthPath
        } else if self.welfareModel.renwu[index].type == "2" {
            let news = HWWelfareNewsController()
            return news
        } else if self.welfareModel.renwu[index].type == "3" {
            let oneYuan = HWOneYuanTradeController()
            return oneYuan
        }
        
        let news = HWWelfareNewsController()
        return news
    }
    
    func tableHeaderViewHeight(in pagingView: JXPagingView) -> Int {

        let topHeight = ceil(Double(self.welfareModel.topdata.count) / 3) * 101*widthScale
        let funcHeight = ceil(Double(self.welfareModel.maindata.count) / 4) * 92*widthScale
        return Int(topHeight + funcHeight + 18*widthScale)

    }
    
    func tableHeaderView(in pagingView: JXPagingView) -> UIView {
        return headView
    }
    
    func heightForPinSectionHeader(in pagingView: JXPagingView) -> Int {
        return Int(41*widthScale)
    }
    
    func viewForPinSectionHeader(in pagingView: JXPagingView) -> UIView {
        return segmentBgView
    }
    
    func numberOfLists(in pagingView: JXPagingView) -> Int {
        return segmentedDataSource.dataSource.count
    }
    
}
extension HWWelfareCenterController:JXSegmentedViewDelegate {
    func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
        
    }
}
